package com.xindaoapp.happypet.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.fragment.Postsfragment;
import com.xindaoapp.happypet.fragment.UIManager;
import com.xindaoapp.happypet.fragment.UserFragment;

/**
 * 搜索页面(搜帖子和用户)
 * 
 * @author xindaoApp
 * 
 */
public class Search2Activity extends com.xindaoapp.happypet.ui.BaseActivity implements
        OnClickListener {

    private TextView tv_search_posts;
    private TextView tv_search_user;
    private ImageView ib_cancel;
    private RelativeLayout layout_switch;


    private UserFragment userFragment;
    private Postsfragment postsfragment;

    private Fragment currentF;

    private FragmentActivity mContext;

    private int fragmentLayout;

    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {

        mContext = this;

        layout_switch = (RelativeLayout) findViewById(R.id.layout_switch);

        tv_search_posts = (TextView) findViewById(R.id.tv_search_posts);
        tv_search_user = (TextView) findViewById(R.id.tv_search_user);
        ib_cancel = (ImageView) findViewById(R.id.ib_cancel);

        tv_search_user.setTextColor(getResources().getColor(R.color.pop_choice_cancel));

        userFragment = UserFragment.Instance(null);
        postsfragment = Postsfragment.Instance(null);

        currentF = postsfragment;

        fragmentLayout = R.id.fragment;

        UIManager.getInstance().addFragment(mContext, postsfragment, false, null, fragmentLayout);

        tv_search_posts.setOnClickListener(this);
        tv_search_user.setOnClickListener(this);
        ib_cancel.setOnClickListener(this);

        tv_search_posts.setSelected(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_cancel:
                finish();
                break;
            case R.id.tv_search_posts:

                layout_switch.setBackgroundResource(R.drawable.search_tiezi);
                if (currentF != postsfragment) {
                    UIManager.getInstance().switchContent(mContext, currentF, postsfragment,
                            fragmentLayout);
                    currentF = postsfragment;
                    index = 0;
                    setSelect();
                }

                break;
            case R.id.tv_search_user:
                layout_switch.setBackgroundResource(R.drawable.search_user);
                if (currentF != userFragment) {
                    UIManager.getInstance().switchContent(mContext, currentF, userFragment,
                            fragmentLayout);
                    currentF = userFragment;
                    index = 1;
                    setSelect();
                }

                break;

            default:
                break;
        }
    }

    private void setSelect() {
        if (index == 0) {
            tv_search_posts.setSelected(true);
            tv_search_posts.setTextColor(getResources().getColor(R.color.white));
            tv_search_user.setSelected(false);
            tv_search_user.setTextColor(getResources().getColor(R.color.pop_choice_cancel));
        } else {
            tv_search_posts.setSelected(false);
            tv_search_posts.setTextColor(getResources().getColor(R.color.pop_choice_cancel));
            tv_search_user.setSelected(true);
            tv_search_user.setTextColor(getResources().getColor(R.color.white));
        }
    }

    @Override
    protected int getContentViewResId() {
        // TODO Auto-generated method stub
        return R.layout.activity_search2;
    }

}
